package com.jj.net.kcp;

import com.jj.message.battle.UserMessage;
import com.jj.net.Channel;
import com.jj.net.Message;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import org.beykery.jkcp.KcpOnUdp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Copyrights: QidianGroup@JJ
 *
 * @author: LuHan
 * @date: 2018/1/15, 10:52
 */
public class KcpChannel implements Channel{
    private static Logger logger = LoggerFactory.getLogger(KcpChannel.class);

    private KcpOnUdp kcpOnUdp;

    public KcpOnUdp getKcpOnUdp() {
        return kcpOnUdp;
    }

    public void setKcpOnUdp(KcpOnUdp kcpOnUdp) {
        this.kcpOnUdp = kcpOnUdp;
    }

    @Override
    public Object remote() {
        if(kcpOnUdp != null )
        {
            return kcpOnUdp.getKcp().getUser();
        }else{
            return null;
        }
    }

    @Override
    public void send(Message msg) {
        if(msg instanceof UserMessage){
            UserMessage userMessage = (UserMessage)msg;

            ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
            userMessage.write2ByteBuf(buf);
            kcpOnUdp.send(buf);
            //or send simple UDP
            //kcpOnUdp.sendFAST(msg.getData());
        }
    }

    @Override
    public void close() {
        //not implement!
    }
}
